/**
 * Created on 24.07.2012
 */
package com.example.utils;

import static com.example.utils.ServletUtils.getIntParameter;
import static org.junit.Assert.assertEquals;

import javax.servlet.http.HttpServletRequest;

import mockit.Expectations;
import mockit.Input;
import mockit.NonStrict;
import mockit.NonStrictExpectations;

import org.junit.Test;

import com.example.WarPrjTest;

/**
 * @author Ciprian Mihalache
 *
 */
public class JmockitVariationsWhileTestingServletUtils extends WarPrjTest {
	private static final String PARAM_NAME = "anything";
	private static final int DEFAULT_VALUE = -101;

	@Test
	public void justCheckTheDefaultValueMechanism(HttpServletRequest request) {
		new Expectations() {
			@Input
			@SuppressWarnings("unused")
			String defaultStringReturn = "abc";

			// {
			// }
		};

		int returnedVal = getIntParameter(request, PARAM_NAME, DEFAULT_VALUE);
		assertEquals(DEFAULT_VALUE, returnedVal);
	}

	@Test
	public void butYouCanOverrideTheDefaultValueForSpecificMethodCalls1(
			final HttpServletRequest request) {
		final String otherParamName = "other";

		new Expectations() {
			@Input
			@SuppressWarnings("unused")
			String defaultStringReturn = "abc";

			{
				request.getParameter(PARAM_NAME);
				result = "5";

				// because we are in "Expectations" block, we need to record
				// all calls to the mocked object, IN ORDER they are performed
				request.getParameter(otherParamName);
			}
		};

		int returnedVal = getIntParameter(request, PARAM_NAME, DEFAULT_VALUE);
		assertEquals(5, returnedVal);

		int secondReturnedVal = getIntParameter(request, otherParamName, DEFAULT_VALUE);
		assertEquals(DEFAULT_VALUE, secondReturnedVal);
	}

	@Test
	public void butYouCanOverrideTheDefaultValueForSpecificMethodCalls2(
			final HttpServletRequest request) {
		final String otherParamName = "other";

		new NonStrictExpectations() {
			@Input
			@SuppressWarnings("unused")
			String defaultStringReturn = "abc";

			{
				request.getParameter(PARAM_NAME);
				result = "5";

				// because we are in "NonStrictExpectations" block,
				// we do not need to record all calls to the mocked object
				// only the ones we are interested in
				// request.getParameter(otherParamName);
			}
		};

		int returnedVal = getIntParameter(request, PARAM_NAME, DEFAULT_VALUE);
		assertEquals(5, returnedVal);

		int secondReturnedVal = getIntParameter(request, otherParamName, DEFAULT_VALUE);
		assertEquals(DEFAULT_VALUE, secondReturnedVal);
	}

	@Test
	public void butYouCanOverrideTheDefaultValueForSpecificMethodCalls3(
			@NonStrict final HttpServletRequest request) {
		final String otherParamName = "other";

		new Expectations() {
			@Input
			@SuppressWarnings("unused")
			String defaultStringReturn = "abc";

			{
				request.getParameter(PARAM_NAME);
				result = "5";

				// We are in "Expectations" block, but we don't need to record
				// all calls to the mocked object, because it is NonStrict
				// request.getParameter(otherParamName);
			}
		};

		int returnedVal = getIntParameter(request, PARAM_NAME, DEFAULT_VALUE);
		assertEquals(5, returnedVal);

		int secondReturnedVal = getIntParameter(request, otherParamName, DEFAULT_VALUE);
		assertEquals(DEFAULT_VALUE, secondReturnedVal);
	}

}
